#include <stdio.h>
#include "epoll.h"

using namespace std;

const char *g_ip = "0.0.0.0";
const int g_port = 8888;
cepoll_data *g_epoll;

void recv_data_cb(cepoll_data *data,socket_data* sock_data)
{
	netpack *t_pack = NULL;
	while((t_pack=pop_queue(&sock_data->m_read_queue)))
	{
		LOG_DBG("recv ip=%s port=%d data=[%s]",sock_data->m_ip,sock_data->m_port,t_pack->m_buffer);
		char *send_buffer = pack_data(t_pack->m_buffer,t_pack->m_len);
		send_data(data,sock_data,send_buffer,t_pack->m_len + 2);
		delete [] t_pack->m_buffer;
		t_pack->m_buffer = NULL;
	}
}

//et epoll
int main(int argc,char *argv[])
{
	g_epoll = create_epoll();
	g_epoll->re_cb = recv_data_cb;
	epoll_event evs[EPOLL_EVENT_MAX];
	listen_sock(g_epoll,g_ip,g_port);
	while(true)
	{
		epoll_loop(g_epoll,evs,EPOLL_EVENT_MAX);
	}
	release_epoll(g_epoll);
	return 0;	
}